
package cn.school.common.utils;

import cn.hutool.core.date.DateUtil;
import cn.school.common.exception.ApiException;
import cn.school.sys.entity.TermTime;

import java.util.*;

/**
 * 课程工具类
 *
 * @className: CourseUtils
 * @author: fangjin.tao
 * @date: 2023/1/6
 **/
public class CourseUtils {
	private static int[] weekDayNo = {7, 1, 2, 3, 4, 5, 6};
	private static String[] weekDayDesc = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
	
	/**
	 * 根据学年学期起始日、周次、星期数，获取日期;
	 *
	 * @throws
	 * @author fangjin.tao
	 * @param: tetiTermBeginDate
	 * @param: coplBeginWeekNo
	 * @param: coplWeekDayNo
	 * @updateTime 2023/1/6 17:00
	 * @return: java.util.Date
	 */
	public static WeekDayVo gennerWeedDayDate(Date tetiTermBeginDate, int coplBeginWeekNo, int coplWeekDayNo) {
		Calendar c = Calendar.getInstance();
		c.setTime(tetiTermBeginDate);
		int index = c.get(Calendar.DAY_OF_WEEK) - 1;
		WeekDayVo weekDayVo = new WeekDayVo();
		weekDayVo.setDate(c.getTime());
		weekDayVo.setWeekDayNo(CourseUtils.weekDayNo[index]);
		weekDayVo.setWeekdayDesc(CourseUtils.weekDayDesc[index]);
		if (!(coplWeekDayNo == weekDayVo.getWeekDayNo() && coplBeginWeekNo == 1)) {
			if (coplBeginWeekNo > 1 && coplWeekDayNo == weekDayVo.getWeekDayNo()) {
				// 满足条件1：第N+1周
				// 满足条件2：[coplWeekDayNo星期数] 与 [学年学期起始日的-星期数]相等
				c.add(Calendar.DAY_OF_MONTH, 7 * (coplBeginWeekNo - 1));
			} else if (coplBeginWeekNo > 1 && coplWeekDayNo != weekDayVo.getWeekDayNo()) {
				// 满足条件1：第N+1周
				// 满足条件2：[coplWeekDayNo星期数] 与 [学年学期起始日的-星期数]不相等
				if (coplWeekDayNo - weekDayVo.getWeekDayNo() > 0) {
					// 指定星期数在，起始日之后
					c.add(Calendar.DAY_OF_MONTH, coplWeekDayNo - weekDayVo.getWeekDayNo());
					c.add(Calendar.DAY_OF_MONTH, 7 * (coplBeginWeekNo - 1));
				} else {
					// 指定星期数在起始日之前
					c.add(Calendar.DAY_OF_MONTH, 7 - weekDayVo.getWeekDayNo() + coplWeekDayNo);
					c.add(Calendar.DAY_OF_MONTH, 7 * (coplBeginWeekNo - 2));
				}
			} else if (coplBeginWeekNo == 1 && coplWeekDayNo != weekDayVo.getWeekDayNo()) {
				// 满足条件1：第1周
				// 满足条件2：[coplWeekDayNo星期数] 与 [学年学期起始日的-星期数]不相等
				if (coplWeekDayNo - weekDayVo.getWeekDayNo() > 0) {
					c.add(Calendar.DAY_OF_MONTH, coplWeekDayNo - weekDayVo.getWeekDayNo());
				} else {
					throw new ApiException("星期数错误：第1周对应的星期数，不能小于" + weekDayVo.getWeekdayDesc());
				}
			} else {
				// 满足条件1：第1周
				// 满足条件2：[coplWeekDayNo星期数] 与 [学年学期起始日的-星期数]不相等
			}
		} else {
			// 此情况不需要处理
			// 满足条件1：是第1周
			// 满足条件2：[星期数] 与 [学年学期起始日期对应的星期数] 相等
		}
		index = c.get(Calendar.DAY_OF_WEEK) - 1;
		weekDayVo.setDate(c.getTime());
		weekDayVo.setWeekDayNo(CourseUtils.weekDayNo[index]);
		weekDayVo.setWeekdayDesc(CourseUtils.weekDayDesc[index]);
		return weekDayVo;
	}
	
	/**
	 * 生成头部
	 *
	 * @throws
	 * @author fangjin.tao
	 * @param: courBeginAreaTime
	 * @param: courEndAreaTime
	 * @updateTime 2023/2/2 10:42
	 * @return: java.util.List<java.util.Map>
	 */
	public static List<Map> gennerClumnTites(Date courBeginAreaTime, Date courEndAreaTime) {
		List<Map> header = new ArrayList<>();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(courBeginAreaTime);
		Map<String, String> rowMap = null;
		while (true) {
			if (calendar.getTime().getTime() >= courEndAreaTime.getTime()) {
				break;
			}
			rowMap = new HashMap<>();
			int index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
			rowMap.put("weekName", CourseUtils.weekDayDesc[index]);
			rowMap.put("day", DateUtil.format(calendar.getTime(), "MM-dd"));
			header.add(rowMap);
			calendar.add(Calendar.DAY_OF_MONTH, 1);
		}
		return header;
	}
	
	/**
	 * 计算学年学期起始日，对应的周一日期
	 *
	 * @throws
	 * @author fangjin.tao
	 * @param: termTime
	 * @updateTime 2023/2/8 14:45
	 * @return: java.util.Date
	 */
	public static Date calcWeekBeginDate(TermTime termTime) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(termTime.getTetiTermBeginDate());
		int weekDay = weekDayNo[calendar.get(Calendar.DAY_OF_WEEK) - 1];
		if (weekDay == 1) {
			return termTime.getTetiTermBeginDate();
		} else {
			calendar.add(Calendar.DAY_OF_MONTH, -(weekDay - 1));
			return calendar.getTime();
		}
	}
	
	/**
	 * 计算学年学期结束日期，对应的星期日的日期
	 *
	 * @throws
	 * @author fangjin.tao
	 * @param: termTime
	 * @updateTime 2023/2/8 14:45
	 * @return: java.util.Date
	 */
	public static Date calcWeekEndDate(TermTime termTime) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(termTime.getTetiTermEndDate());
		int weekDay = weekDayNo[calendar.get(Calendar.DAY_OF_WEEK) - 1];
		if (weekDay == 7) {
			return termTime.getTetiTermEndDate();
		} else {
			calendar.add(Calendar.DAY_OF_MONTH, 7 - weekDay);
			return calendar.getTime();
		}
	}
}
